package com.mujin.keji.collectionsystem.base;

import com.mujin.keji.collectionsystem.http.CommonHttpSubscriber;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Repository基类
 *
 * @author weishixiong
 * @Time 2018-03-30
 */

public class CommonRepository<T> {
    /**
     * RxJava Subscriber回调
     */
    private CommonHttpSubscriber<T> commonHttpSubscriber;
    /**
     * 解决背压
     */
    private Flowable<BaseDto<T>> flowable;

    /**
     * 发送请求服务器事件
     */
    public CommonHttpSubscriber<T> send() {
       // LoginDto dto = SPUtil.getData(Constant.SP.SP, Constant.SP.LOGIN_KEY, LoginDto.class, null);
        //如果是登录请求，放行
      //  if (EmptyUtils.isNotEmpty(dto) || Constant.Server.isLoginRequest) {
            flowable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(commonHttpSubscriber);
        //}
        return commonHttpSubscriber;
    }

    /**
     * 初始化
     * commonHttpSubscriber = new Common
     */
    public CommonRepository() {
        commonHttpSubscriber = new CommonHttpSubscriber<>();
    }

    /**
     * 初始化flowable
     *
     * @param flowable
     * @return
     */
    public CommonRepository<T> request(Flowable<BaseDto<T>> flowable) {
        this.flowable = flowable;
        return this;
    }

}
